﻿// See https://aka.ms/new-console-template for more information
using static System.Console;

Title = "C#中闭包";

List<Action> actions = new List<Action>();

//for(int i = 0; i < 6; i++)
//{
//    actions.Add(() => WriteLine(i));
//}


//foreach (Action action in actions)
//{
//    action();
//}



//for (int i = 0; i < 6; i++)
//{
//    int j = i;
//    actions.Add(() => WriteLine(j));
//}


//foreach (Action action in actions)
//{
//    action();
//}


//task任务
//for(int i = 0; i < 10; i++)
//{
//    Task.Run(() => WriteLine(i));
//}


for (int i = 0; i < 5; i++)
{
    Task task = Task.Run(() => WriteLine(i));
    task.Wait();

}


WriteLine("---------");
int[] arr = { 1, 2, 3, 4, 5, 6 };
foreach (var item in arr)
{
    actions.Add(() => WriteLine(item));
}


foreach (Action item in actions)
{
    item();
}

ReadKey();

